package com.yang.IO;

import org.junit.Test;

import java.io.*;

/**
 * @Description: TODO
 * @Author: YangTing
 * @Date: 2024-05-06 21:31
 */

public class BufferedStream {
    @Test
    public void test1(){
        File srcFile = new File("1.jpg");
        File descFile = new File("1_copy.jpg");
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            fileInputStream = new FileInputStream(srcFile);
            fileOutputStream = new FileOutputStream(descFile);
            bis = new BufferedInputStream(fileInputStream);
            bos = new BufferedOutputStream(fileOutputStream);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = bis.read(buffer)) != -1){
                bos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                assert fileInputStream != null;
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                assert fileOutputStream != null;
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void test2() throws UnsupportedEncodingException {
        byte[] b = {'a','b','1','c','8'};
        String s = new String(b,2,2,"gbk");
        System.out.println("s = " + s);
    }
}
